package create.com.modelproaction.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import create.com.modelproaction.R;
import create.com.modelproaction.entity.Homemenubean;

////    Android RecyclerView 的瀑布流式布局
////    https://blog.51cto.com/u_15757710/5602545
//}
public class StaggeredRecyclerAdapter

        extends RecyclerView.Adapter<StaggeredRecyclerAdapter.ViewHolder> {

    private static final String TAG = "TengXunViewAdapter";
    private Context mContext;
    List<Homemenubean> mFruitList;


    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener listener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    public interface OnItemLongClickListener {
        void onClick(int position);
    }

    private OnItemLongClickListener longClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }
//    点击事件

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;
        LinearLayout linearLayout;

        public ViewHolder(View view) {
            super(view);
            linearLayout = (LinearLayout) view;
            fruitImage = (ImageView) view.findViewById(R.id.card_image);
            fruitName = (TextView) view.findViewById(R.id.card_title);
        }
    }

    //    List<Homemenubean> homeNumList
    public StaggeredRecyclerAdapter(List<Homemenubean> fruitList) {
        mFruitList = fruitList;
    }


    ImageView mImageView;
    TextView mTextView;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_item_card, parent, false);

        final ViewHolder holder = new ViewHolder(view);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
            }
        });
        int parentHeight = parent.getHeight();
        parent.getWidth();
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.height = (parentHeight / 3);
        return holder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Homemenubean fruit = mFruitList.get(position);
        holder.fruitName.setText(mFruitList.get(position).getHomeMeName());

        Glide.with(mContext)
                .load(fruit.getHomeMeImage())
                .placeholder(R.mipmap.haibao01)//默认图
//                .error(R.drawable.selector_list_item)//发生错误的默认图
                .into(holder.fruitImage);
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
        holder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null) {
                    longClickListener.onClick(position);
                }
                return true;
            }
        });


    }


    @Override
    public int getItemCount() {
        {
            return mFruitList.size();
        }
    }
}